package com.dd.service;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.dd.dao.DdShopDAO;
import com.dd.dao.DictDAO;
import com.dd.entity.DdShop;
import com.dd.entity.Dict;

public class ShopSupportService {
	private DdShopDAO ddShopDAO;
	private DictDAO dictDAO;

	public DictDAO getDictDAO() {
		return dictDAO;
	}

	public void setDictDAO(DictDAO dictDAO) {
		this.dictDAO = dictDAO;
	}

	public DdShopDAO getDdShopDAO() {
		return ddShopDAO;
	}

	public void setDdShopDAO(DdShopDAO ddShopDAO) {
		this.ddShopDAO = ddShopDAO;
	}

	
	public void newShop(HttpServletRequest request, DdShop ds, StringBuffer sbf)
			throws UnsupportedEncodingException {

		if (request.getParameter("type") != null) {
			ds.setType(Integer.parseInt(request.getParameter("type")));
		}
		if (request.getParameter("name") != null) {
			ds.setName(request.getParameter("name"));
		}
		if (request.getParameter("shophours") != null) {
			ds.setShophours(request.getParameter("shophours"));
		}
		if (request.getParameter("style") != null) {
			String style = request.getParameter("style");
			String styleStr[] = style.split(",");
			Long countCode = null;
			for (int i = 0; i < styleStr.length; i++) {
				Dict dict = this.getDictDAO().findByTypeAndValue(2,
						String.valueOf(styleStr[i]));
				long code = dict.getNodeCode();
				List dictList = this.getDictDAO().findByRootNode(
						Integer.valueOf(String.valueOf(code)));
				for (int j = 0; i < dictList.size(); j++) {
					Dict dict2 = (Dict) dictList.get(j);
					countCode += dict2.getNodeCode();
				}
			}
			ds.setStyle(countCode);
		}

		if (request.getParameter("styletype") != null) {
			String styletype = request.getParameter("styletype");
			Dict dict = this.getDictDAO().findByTypeAndValue(2, styletype);
			ds
					.setStyleType(Integer.valueOf(String.valueOf(dict
							.getNodeCode())));
		}
		if (request.getParameter("cost") != null) {
			String cost = request.getParameter("cost");
			Dict dict = this.getDictDAO().findByTypeAndValue(2, cost);
			ds
					.setStyleType(Integer.valueOf(String.valueOf(dict
							.getNodeCode())));
		}
		if (request.getParameter("phone") != null) {
			ds.setPhone(request.getParameter("phone"));
		}
		if (request.getParameter("mobile") != null) {
			ds.setMobile(request.getParameter("mobile"));
		}
		if (request.getParameter("address") != null) {
			ds.setAddress(request.getParameter("address"));
		}
		if (request.getParameter("area") != null) {
			String area = request.getParameter("area");
			Dict dict = this.getDictDAO().findByTypeAndValue(2, area);
			ds.setArea(String.valueOf(dict.getNodeCode()));
		}
		if (request.getParameter("road") != null) {
			ds.setRoad(request.getParameter("road"));
		}
		if (request.getParameter("landmark") != null) {
			ds.setLandmark(request.getParameter("landmark"));
		}
		if (request.getParameter("range") != null) {
			ds.setRange(request.getParameter("range"));
		}
		if (request.getParameter("bus") != null) {
			ds.setBus(request.getParameter("bus"));
		}
		if (request.getParameter("parking") != null) {
			ds.setParking(request.getParameter("parking"));
		}
		if (request.getParameter("description") != null) {
			ds.setDescription(request.getParameter("description"));
		}
		if (request.getParameter("purpose") != null) {
			String purpose = request.getParameter("purpose");
			String purposeStr[] = purpose.split(",");
			Long countCode = null;
			for (int i = 0; i < purposeStr.length; i++) {
				Dict dict = this.getDictDAO().findByTypeAndValue(2,
						String.valueOf(purposeStr[i]));
				long code = dict.getNodeCode();
				List dictList = this.getDictDAO().findByRootNode(
						Integer.valueOf(String.valueOf(code)));
				for (int j = 0; i < dictList.size(); j++) {
					Dict dict2 = (Dict) dictList.get(j);
					countCode += dict2.getNodeCode();
				}
			}
			ds.setPurpose(Integer.parseInt(String.valueOf(countCode)));
		}
		if (request.getParameter("service") != null) {
			String service = request.getParameter("service");
			String serviceStr[] = service.split(",");
			Long countCode = null;
			for (int i = 0; i < serviceStr.length; i++) {
				Dict dict = this.getDictDAO().findByTypeAndValue(2,
						String.valueOf(serviceStr[i]));
				long code = dict.getNodeCode();
				List dictList = this.getDictDAO().findByRootNode(
						Integer.valueOf(String.valueOf(code)));
				for (int j = 0; i < dictList.size(); j++) {
					Dict dict2 = (Dict) dictList.get(j);
					countCode += dict2.getNodeCode();
				}
			}
			ds.setService(String.valueOf(countCode));
		}
		if (request.getParameter("number") != null) {
			ds.setNumber(request.getParameter("number"));
		}
		if (request.getParameter("numbertype") != null) {
			ds.setNumbertype(request.getParameter("numbertype"));
		}
		if (request.getParameter("belong") != null) {
			ds.setBelong(Integer.valueOf(request.getParameter("belong")));
		}
		if (request.getParameter("pic1") != null) {
			ds.setPic1(request.getParameter("pic1"));
		}
		if (request.getParameter("pic2") != null) {
			ds.setPic2(request.getParameter("pic2"));
		}
		if (request.getParameter("pic3") != null) {
			ds.setPic3(request.getParameter("pic3"));
		}
		if (request.getParameter("pic4") != null) {
			ds.setPic4(request.getParameter("pic4"));
		}
		if (request.getParameter("pic5") != null) {
			ds.setPic5(request.getParameter("pic5"));
		}
		if (request.getParameter("pic6") != null) {
			ds.setPic6(request.getParameter("pic6"));
		}
		if (request.getParameter("pic7") != null) {
			ds.setPic7(request.getParameter("pic7"));
		}
		if (request.getParameter("pic8") != null) {
			ds.setPic8(request.getParameter("pic8"));
		}
		if (request.getParameter("hot") != null) {
			ds.setHot(Integer.valueOf(request.getParameter("hot")));
		}
		if (request.getParameter("issend") != null) {
			ds.setIssend(Integer.valueOf(request.getParameter("issend")));
		}
		if (request.getParameter("isbooking") != null) {
			ds.setIsbooking(Integer.valueOf(request.getParameter("isbooking")));
		}
		if (request.getParameter("suggest") != null) {
			ds.setSuggest(request.getParameter("suggest"));
		}
		this.getDdShopDAO().saveOrUpdate(ds);
		sbf.append("<result>1</result></root>");
	}
}
